package view.invoice;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;

import controller.invoice.EditViewController;
import controller.invoice.InvoiceViewController;

public class InvoiceViewPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private JButton btnInvviewDelete;
	private JButton btnInvviewEdit;
	private JButton btnInvviewPrint;
	private JButton btnInvviewSearch;
	private JComboBox<String> cbbInvviewSearchby;
	private JScrollPane jScrollPane1;
	private JScrollPane jScrollPane4;
	private JLabel lblInvviewBy;
	private JPanel pnInnviewListinvoice;
	private JPanel pnInvviewInvoicedetail;
	private JTable tblInnviewListinvoice;
	private JTable tblInvviewInvoicedetail;
	private JTextField txtInvviewSearch;

	private InvoiceViewController controller;
	private static InvoiceViewPanel inst;

	public static final InvoiceViewPanel getInst() {
		if (inst == null) {
			return inst = new InvoiceViewPanel();
		}
		return inst;

	}

	public InvoiceViewController getController() {
		return this.controller;
	}

	public InvoiceViewPanel() {
		initComponents();
		events();

	}

	private void initComponents() {

		pnInvviewInvoicedetail = new JPanel();
		jScrollPane1 = new JScrollPane();
		tblInvviewInvoicedetail = new JTable();
		btnInvviewDelete = new JButton();
		btnInvviewEdit = new JButton();
		btnInvviewPrint = new JButton();
		pnInnviewListinvoice = new JPanel();
		jScrollPane4 = new JScrollPane();
		tblInnviewListinvoice = new JTable();
		txtInvviewSearch = new JTextField();
		lblInvviewBy = new JLabel();
		cbbInvviewSearchby = new JComboBox<String>();
		btnInvviewSearch = new JButton();

		controller = new InvoiceViewController(this);
		pnInvviewInvoicedetail.setBackground(new java.awt.Color(255, 255, 255));
		pnInvviewInvoicedetail.setBorder(BorderFactory.createTitledBorder(null,
				"Invoice detail", TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION, new java.awt.Font("Calibri", 1,
						12), java.awt.Color.blue));

		tblInvviewInvoicedetail.setFont(new java.awt.Font("Calibri", 1, 12));
		tblInvviewInvoicedetail
				.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		jScrollPane1.setViewportView(tblInvviewInvoicedetail);

		btnInvviewDelete.setIcon(new ImageIcon("icon/em_delete.png"));
		btnInvviewDelete.setText("Delete");
		btnInvviewDelete.addActionListener(controller);

		btnInvviewEdit.setIcon(new ImageIcon("icon/em_edit.png"));
		btnInvviewEdit.setText("Edit");
		btnInvviewEdit.setVisible(false);

		btnInvviewPrint.setIcon(new ImageIcon("icon/print.png"));
		btnInvviewPrint.setText("Export...");

		GroupLayout pn_invview_invoicedetailLayout = new GroupLayout(
				pnInvviewInvoicedetail);
		pnInvviewInvoicedetail.setLayout(pn_invview_invoicedetailLayout);
		pn_invview_invoicedetailLayout
				.setHorizontalGroup(pn_invview_invoicedetailLayout
						.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(jScrollPane1,
								GroupLayout.Alignment.TRAILING)
						.addGroup(
								GroupLayout.Alignment.TRAILING,
								pn_invview_invoicedetailLayout
										.createSequentialGroup()
										.addContainerGap(
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addComponent(btnInvviewPrint,
												GroupLayout.PREFERRED_SIZE, 110,
												GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(btnInvviewEdit,
												GroupLayout.PREFERRED_SIZE, 90,
												GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(btnInvviewDelete,
												GroupLayout.PREFERRED_SIZE, 90,
												GroupLayout.PREFERRED_SIZE)
										.addContainerGap()));
		pn_invview_invoicedetailLayout
				.setVerticalGroup(pn_invview_invoicedetailLayout
						.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addGroup(
								GroupLayout.Alignment.TRAILING,
								pn_invview_invoicedetailLayout
										.createSequentialGroup()
										.addComponent(jScrollPane1,
												GroupLayout.DEFAULT_SIZE, 165,
												Short.MAX_VALUE)
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												pn_invview_invoicedetailLayout
														.createParallelGroup(
																GroupLayout.Alignment.BASELINE)
														.addComponent(
																btnInvviewDelete)
														.addComponent(
																btnInvviewEdit)
														.addComponent(
																btnInvviewPrint))
										.addContainerGap()));

		pnInnviewListinvoice.setBackground(new java.awt.Color(255, 255, 255));
		pnInnviewListinvoice.setBorder(BorderFactory.createTitledBorder(null,
				"List invoice", TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION, new java.awt.Font("Calibri", 1,
						12), java.awt.Color.blue));

		tblInnviewListinvoice.setFont(new java.awt.Font("Calibri", 1, 12));
		tblInnviewListinvoice
				.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		jScrollPane4.setViewportView(tblInnviewListinvoice);

		lblInvviewBy.setText("by");

		cbbInvviewSearchby.setModel(new DefaultComboBoxModel<String>(
				new String[] { "InvoiceID", "Orther" }));

		btnInvviewSearch.setFont(new java.awt.Font("Calibri", 1, 12));
		btnInvviewSearch.setIcon(new ImageIcon("icon/search.png"));
		btnInvviewSearch.setText("Search");
		btnInvviewSearch.addActionListener(controller);
		GroupLayout pn_innview_listinvoiceLayout = new GroupLayout(
				pnInnviewListinvoice);
		pnInnviewListinvoice.setLayout(pn_innview_listinvoiceLayout);
		pn_innview_listinvoiceLayout
				.setHorizontalGroup(pn_innview_listinvoiceLayout
						.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addGroup(
								GroupLayout.Alignment.TRAILING,
								pn_innview_listinvoiceLayout
										.createSequentialGroup()
										.addContainerGap(
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addComponent(txtInvviewSearch,
												GroupLayout.PREFERRED_SIZE,
												151, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(lblInvviewBy)
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(cbbInvviewSearchby,
												GroupLayout.PREFERRED_SIZE, 79,
												GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(btnInvviewSearch,
												GroupLayout.PREFERRED_SIZE, 90,
												GroupLayout.PREFERRED_SIZE)
										.addContainerGap())
						.addComponent(jScrollPane4,
								GroupLayout.Alignment.TRAILING,
								GroupLayout.DEFAULT_SIZE, 874, Short.MAX_VALUE));
		pn_innview_listinvoiceLayout
				.setVerticalGroup(pn_innview_listinvoiceLayout
						.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addGroup(
								GroupLayout.Alignment.TRAILING,
								pn_innview_listinvoiceLayout
										.createSequentialGroup()
										.addGroup(
												pn_innview_listinvoiceLayout
														.createParallelGroup(
																GroupLayout.Alignment.BASELINE)
														.addComponent(
																txtInvviewSearch,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																lblInvviewBy)
														.addComponent(
																cbbInvviewSearchby,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																btnInvviewSearch))
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(jScrollPane4,
												GroupLayout.DEFAULT_SIZE, 159,
												Short.MAX_VALUE)));

		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.LEADING)
										.addComponent(pnInvviewInvoicedetail,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addComponent(pnInnviewListinvoice,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE))
						.addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(
				GroupLayout.Alignment.LEADING)
				.addGroup(
						GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(pnInnviewListinvoice,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(pnInvviewInvoicedetail,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE).addContainerGap()));
	}

	public JButton getBtnInvviewDelete() {
		return btnInvviewDelete;
	}

	public JButton getBtnInvviewEdit() {
		return btnInvviewEdit;
	}

	public JButton getBtnInvviewPrint() {
		return btnInvviewPrint;
	}

	public JButton getBtnInvviewSearch() {
		return btnInvviewSearch;
	}

	public JComboBox<String> getCbbInvviewSearchby() {
		return cbbInvviewSearchby;
	}

	public JLabel getLblInvviewBy() {
		return lblInvviewBy;
	}

	public JPanel getPnInnviewListinvoice() {
		return pnInnviewListinvoice;
	}

	public JPanel getPnInvviewInvoicedetail() {
		return pnInvviewInvoicedetail;
	}

	public JTable getTblInnviewListinvoice() {
		return tblInnviewListinvoice;
	}

	public JTable getTblInvviewInvoicedetail() {
		return tblInvviewInvoicedetail;
	}

	public JTextField getTxtInvviewSearch() {
		return txtInvviewSearch;
	}

	private void events() {
		btnInvviewEdit.addActionListener(new EditViewController());
		tblInnviewListinvoice.addMouseListener(controller);
		btnInvviewPrint.addActionListener(controller);
	}

}
